package com.xszx.serviceedu.controller;


import com.xszx.commonutils.R;
import com.xszx.serviceedu.entity.Chapter;
import com.xszx.serviceedu.entity.vo.ChapterVo;
import com.xszx.serviceedu.service.ChapterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author 白熊
 * @since 2024-07-17
 */
@RestController
@RequestMapping("/serviceedu/chapter")
//@CrossOrigin
@Api(tags = "章节管理")
public class ChapterController {

    @Autowired
    private ChapterService chapterService;

    @PutMapping("{id}")
    @ApiOperation("根据id修改章目录")
    public R updateById(@PathVariable
                        @ApiParam(name = "id",value = "章目录id",required = true) String id,
                        @ApiParam(name = "chapter", value = "章节对象", required = true)
                        @RequestBody  Chapter chapter){
        chapterService.updateById(chapter);
        return R.ok().message("章节修改成功");
    }
    @DeleteMapping("{id}")
    @ApiOperation("根据id删除章目录")
    public R removeById(@ApiParam(name = "id",value = "章节id",required = true)
                         @PathVariable String id){
        Boolean chapter = chapterService.removeChapterById(id);
        if(chapter){
            return R.ok().message("删除成功!");
        }else {
            return R.error().message("删除失败");
        }
    }


    @GetMapping("/getById/{id}")
    @ApiOperation("根据id回显章节目录详细信息")
    public R getById(@ApiParam(value = "章目录id",name = "id",required = true)
                                 @PathVariable String id){
        Chapter chapter = chapterService.getById(id);
        return R.ok().data("item",chapter);
    }

    @PostMapping
    @ApiOperation("新增章节")
    public R save(@ApiParam(name = "chapter",value = "章节对象",required = true)
                              @RequestBody Chapter chapter){
        chapterService.save(chapter);
        return R.ok().message("添加章节成功");
    }

    @GetMapping("{courseId}")
    @ApiOperation("根据课程id获取章节目录")
    public R nestedListByCourseId(@PathVariable
                                   @ApiParam(name = "courseId",value = "课程id",required = true)
                                   String courseId){
        List<ChapterVo> chapterVoList = chapterService.nestedList(courseId);
        return R.ok().data("items",chapterVoList);

    }
}

